﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MarkdEdit.markdown
{

    public class MarkDownDoc
    {
        public const string CssTemplate = "<style type=\"text/css\">{0}</style>";
        public const string htmlTemplate1 = @"
<!DOCTYPE html>
    <head>
        <meta http-equiv=""Content-Type"" content=""text/html;charset=utf-8"" />
        <meta http-equiv=""X-UA-Compatible"" content=""IE=edge"" />
        {0}
    </head>
    <body>
        {1}
    </body>
</html>";
        public const string htmlTemplate2 = @"
<!DOCTYPE html>
    <head>
        <meta http-equiv=""Content-Type"" content=""text/html;charset=utf-8"" />
        {0}
    </head>
    <body>
        {1}
    </body>
</html>";

        public const string MathScript = @"
<script type=""text/javascript"" src=""http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML""></script>
<script type = ""text/x-mathjax-config"" >
    MathJax.Hub.Config({ tex2jax: {inlineMath: [['$', '$']]}, messageStyle: ""none"" });
</script>
";
        public const string LocalKatexScript = @"
    <link rel=""stylesheet"" href=""html/katex/katex.min.css"">
    <!-- The loading of KaTeX is deferred to speed up page rendering -->
    <script defer src=""katex/katex.min.js""></script>
    <!-- To automatically render math in text elements, include the auto-render extension: -->
    <script defer src=""katex/contrib/auto-render.min.js""></script>
    <script>
        document.addEventListener(""DOMContentLoaded"", function()
        {
            renderMathInElement(document.body, {
            // customised options
            // • auto-render specific keys, e.g.:
            delimiters:
                [
                    { left: '$$', right: '$$', display: true},
                    { left: '$', right: '$', display: false},
                    { left: '\\(', right: '\\)', display: false},
                    { left: '\\[', right: '\\]', display: true}
                ],
                // • rendering keys, e.g.:
                throwOnError: false
            });
        });
    </script>
";


        public static string WrapHtmlText(string htmlText, params string[] cssStrs)
        {
            var ret = "";
            var headstr = new StringBuilder();
            if (cssStrs != null)
            {
                for (int i = 0; i < cssStrs.Length; i++)
                {
                    headstr.AppendLine(string.Format(CssTemplate, cssStrs[i]));
                }
            }
            headstr.AppendLine(headstr.ToString());
            headstr.AppendLine(CSSConsts.KatexScript);
            ret = string.Format(htmlTemplate1, headstr.ToString(), htmlText);
            return ret;
        }

        public static string WrapHtmlText_Math(string htmlText, params string[] cssStrs)
        {
            var ret = "";
            var headstr = new StringBuilder();
            if (cssStrs != null)
            {
                for (int i = 0; i < cssStrs.Length; i++)
                {
                    headstr.AppendLine(string.Format(CssTemplate, cssStrs[i]));
                }
            }
            headstr.AppendLine(CSSConsts.KatexScript);
            ret = string.Format(htmlTemplate1, headstr.ToString(), htmlText);
            return ret;
        }
    }
}